package com.example.sharesinfo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.Toast;

import com.example.sharesinfo.R;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.example.sharesinfo.utils.SpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class FeedBackActivity extends BaseUIActivity implements View.OnClickListener {
    private static final String LOGTAG = "FeedBackActivity:";
    private static final int SendFeedbackSuccess = 1001;
    private static final int SendFeedbackFailed = 1002;
    private ImageButton ib_return;
    private EditText fee_back_edit;
    private ScrollView scrollview;
    private Button feed_back_btn;

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case SendFeedbackSuccess:
                    Toast.makeText(FeedBackActivity.this, "我们已经收到您的反馈，我们会尽快处理。", Toast.LENGTH_SHORT).show();
                    fee_back_edit.setText("");
                    break;
                case SendFeedbackFailed:
                    Toast.makeText(FeedBackActivity.this, "反馈失败，请重试。", Toast.LENGTH_SHORT).show();
                    break;
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        initView();
    }

    private void initView() {
        ib_return = findViewById(R.id.ib_return);
        ib_return.setOnClickListener(this);
        fee_back_edit = findViewById(R.id.fee_back_edit);
        scrollview = findViewById(R.id.scrollview);
        feed_back_btn = findViewById(R.id.feed_back_btn);
        feed_back_btn.setOnClickListener(this);
    }

    /**
     * 用户反馈
     * Post
     * 用户id:UserId
     * 反馈内容:Content
     */
    private void sendFeedback(String feedback) {
        String urlRoot = "http://119.23.221.142/api/user/SendFeedback?";
        Map<String, String> datas = new HashMap<>();
        datas.put("UserId", SpUtils.getInstance().getString(Constants.SP_USER_ID, ""));
        datas.put("Content", feedback);
        OkHttpUtils.getInstance().Post(urlRoot, null, datas,
                null, null, new OkHttpUtils.okHttpListener() {
                    Message message = mHandler.obtainMessage();

                    @Override
                    public void OnSuccess(String s, int code) throws JSONException {
                        JSONObject object = new JSONObject(s);
                        if (object.getString("code").equals("200")) {
                            message.what = SendFeedbackSuccess;
                            mHandler.sendMessage(message);
                            LogUtils.i(LOGTAG + s);
                        } else {
                            message.what = SendFeedbackFailed;
                            mHandler.sendMessage(message);
                        }
                    }

                    @Override
                    public void OnFailed(String s) {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.ib_return:
                finish();
                break;
            case R.id.feed_back_btn:
                String feedback = fee_back_edit.getText().toString().trim();
                if (TextUtils.isEmpty(feedback)) {
                    return;
                }
                sendFeedback(feedback);
                break;
        }
    }


}
